﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AtualizadorScripts
{
    public class Initial
    {
        public string Conection { get; set; }

        #region LoadParameters

        public DataTable LoadParameters()
        {

            DataTable data = new DataTable();
            //string parametro = "VERSAO_SERVICE_PACK_LOJA";
            try
            {
                using (SqlConnection connection = Conexao.getConnection())
                {
                    connection.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT PARAMETRO, VALOR_ATUAL FROM PARAMETROS ", connection))
                    {

                        try
                        {

                            //cmd.Parameters.AddWithValue("@parametro", parametro);
                            SqlDataAdapter adaptador = new SqlDataAdapter(cmd);

                            adaptador.Fill(data); // Preenche a tabela

                        }
                        catch (Exception)
                        {

                            throw;
                        }

                    }
                }
            }

            catch (SqlException sqlExc)
            {
                MessageBox.Show(sqlExc.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return data;
        }


        #endregion

        #region SaveParameters
        public void SaveParameters(string parametro, string valor_atual)
        {
            try
            {
                using (SqlConnection connection = Conexao.getConnection())
                {
                    connection.Open();
                    using (SqlCommand cmd = new SqlCommand("UPDATE PARAMETROS SET VALOR_ATUAL = @valor_atual " +
                        "where parametro = @parametro", connection))
                    {
                        try
                        {
                           cmd.Parameters.AddWithValue("@parametro", parametro);
                           cmd.Parameters.AddWithValue("@valor_atual", valor_atual);
                           cmd.ExecuteNonQuery();
                            //SqlDataAdapter adaptador = new SqlDataAdapter(cmd);
                        }
                        catch (Exception)
                        {

                            throw;
                        }

                    }
                }

            }
            catch (SqlException sqlExc)
            {
                MessageBox.Show(sqlExc.Message, "Erro ao executar update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #endregion
    }
}
